﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SPMS.Common;
using SPMS.Data;
using SPMS.Web.Mvc;

namespace SPMS.Web.Controllers
{
    /// <summary>
    /// 作业类型控制器。
    /// </summary>
    public class OperationTypeController : Controller
    {
        #region ---Views

        /// <summary>
        /// 默认视图。
        /// </summary>
        /// <returns>视图结果。</returns>
        public ActionResult Index()
        {
            return View();
        }

        #endregion

        #region ---Actions

        /// <summary>
        /// 获得操作类型信息列表。
        /// </summary>
        /// <param name="queryModel">查询模型对象。</param>
        /// <returns>数据。</returns>
        [JsonHandleError]
        public ActionResult List(JsonPagingQueryModel queryModel)
        {
            //创建上下文
            using (AccountFileManagementEntities context = new AccountFileManagementEntities())
            {
                //用于保存总记录数
                int totalRecord = 0;

                //查询数据
                var query = from item in context.OperationTypes.QueryData<OperationType>(queryModel, out totalRecord).ToArray()
                            select new
                            {
                                item.OperationType_ID,
                                item.OperationType_Code,
                                item.OperationType_Name
                            };

                //获得数据
                var data = query.ToArray();

                //返回数据
                return this.JsonEx(true, data, totalRecord);
            }
        }

        #endregion

        #region ---Public Method

        /// <summary>
        /// 获得操作类型JSON字符串。
        /// </summary>
        /// <returns>操作类型JSON字符串。</returns>
        public static string GetOperationTypeJSON()
        {
            //用于保存数据
            List<string> data = new List<string>();

            //查询项目类型信息
            using (AccountFileManagementEntities entities = new AccountFileManagementEntities())
            {
                //查询所有养护类型信息
                foreach (OperationType item in entities.OperationTypes.Where(item => item.IsEnabled).OrderBy(item => item.OperationType_Code))
                {
                    //转换为字符串
                    data.Add(string.Format(@"{{ id: '{0}', code: '{1}', name:'{1} {2}' }}", item.OperationType_ID, item.OperationType_Code, item.OperationType_Name));
                }
            }

            //返回字符串
            return System.String.Join(",", data);
        }

        #endregion
    }
}
